﻿using System;
using System.Windows.Forms;
using cndp.Model;

namespace cndp.View
{
    public partial class FormShowJudges : Form
    {
        private string _oldValue;
        private int _rowOfJudge;

        public FormShowJudges()
        {
            InitializeComponent();
            dataGridViewContestJudgeRegister.Columns["personalCodeNumberDataGridViewTextBoxColumn"].ReadOnly = true;
        }

        private void FormShowJudges_Load(object sender, EventArgs e)
        {
            KeyPreview = true;
            if (dataGridViewContestJudgeRegister.Rows.Count != 0)
            {
                _rowOfJudge = 0;
            }
        }

        private void dataGridViewContestJudgeRegister_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            _rowOfJudge = e.RowIndex;
        }

        private void dataGridViewContestJudgeRegister_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            _oldValue = dataGridViewContestJudgeRegister[e.ColumnIndex, e.RowIndex].Value.ToString();
        }

        private void dataGridViewContestJudgeRegister_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            var d = new Datacheck();
            var row = dataGridViewContestJudgeRegister[e.ColumnIndex, e.RowIndex];
            if (row.ColumnIndex == 0 || row.ColumnIndex == 1 || row.ColumnIndex == 4)
            {
                if (d.CheckIfContainNumbers(row.Value.ToString()))
                {
                    MessageBox.Show(@"Fel format.", @"Ogiltigt data.");
                    row.Value = _oldValue;
                }
            }
            if (row.ColumnIndex == 2)
            {
                if (!(row.Value.ToString() == "Herr" || row.Value.ToString() == "Dam"))
                {
                    MessageBox.Show(@"Fel format.", @"Ogiltigt data.");
                    row.Value = _oldValue;
                }
            }
            if (row.ColumnIndex == 3)
            {
                if (!d.CheckIfBirthDay(row.Value.ToString()))
                {
                    MessageBox.Show(@"Fel format.", @"Ogiltigt data.");
                    row.Value = _oldValue;
                }
            }
        }

        private void FormShowJudges_KeyDown(object sender, KeyEventArgs e)
        {
            _rowOfJudge = CheckWhichRowIsMarked();
            if (e.KeyData == Keys.Escape)
                Close();
            if (e.KeyCode == Keys.Delete)
                if (_rowOfJudge == (dataGridViewContestJudgeRegister.Rows.Count - 1))
                {
                    dataGridViewContestJudgeRegister.Rows.RemoveAt(_rowOfJudge);
                    _rowOfJudge--;
                    if (_rowOfJudge != -1)
                    {
                        dataGridViewContestJudgeRegister.Rows[_rowOfJudge].Cells[0].Selected = true;
                    }
                }
                else
                {
                    dataGridViewContestJudgeRegister.Rows.RemoveAt(_rowOfJudge);
                }

            if (e.KeyCode == Keys.Up)
                if ((_rowOfJudge != 0) && (_rowOfJudge != -1))
                {
                    _rowOfJudge--;
                }
            if (e.KeyCode == Keys.Down)
                if (_rowOfJudge != (dataGridViewContestJudgeRegister.Rows.Count - 1))
                {
                    _rowOfJudge++;
                }
            if (e.KeyData == Keys.Enter)
                if (_rowOfJudge != dataGridViewContestJudgeRegister.Rows.Count)
                {
                    _rowOfJudge++;
                }
        }

        private int CheckWhichRowIsMarked()
        {
            var selectedRow = dataGridViewContestJudgeRegister.CurrentCell.RowIndex;
            return selectedRow;
        }
    }
}